home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / export / outbound / outbounduiwin.c < prev   
Encoding:
C/C++ Source or Header  |  1996-10-28  |  10.3 KB  |  409 lines

  1. /*
  2.     File: OutboundUIWin.c
  3.  
  4.     Copyright (c) 1990-1, Thomas Knoll.
  5.     Copyright (c) 1992-6, Adobe Systems Incorporated.
  6.     All rights reserved.
  7.  
  8.     Windows platform dependent C source file for Export example.
  9. */
  10.  
  11. #include "Outbound.h"
  12. #include "WinDialogUtils.h"
  13.  
  14. /*
  15. #include "PITypes.h"
  16. #include "PIgeneral.h"
  17. #include "PIAbout.h"
  18. #include "ExportUtilities.h"
  19.  
  20. #include "windows.h"
  21. #include "errno.h"
  22. #include "string.h"
  23.  
  24. #include "WinUtil.h"
  25. */
  26.  
  27. BOOL WINAPI ExportProc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam);
  28.  
  29. /*****************************************************************************/
  30.  
  31. void DoAbout (GPtr globals)
  32. {
  33.     ShowAbout((AboutRecordPtr)gStuff, hDllInstance, AboutID);
  34. }
  35.  
  36. /*****************************************************************************/
  37.  
  38. Boolean QueryForExportFile (GPtr globals)
  39. {
  40.     char *my_lpstrFile = (char *)gStuff->filename;
  41.     char my_filename[256];
  42.     char my_lpstrFileTitle[256];
  43.     char *my_extension = ".exp";
  44.     char my_lpstrInitialDir[256];
  45.     char my_dir[256];
  46.     char *my_lpstrDefExt = "exp";
  47.     char *my_lpstrFilter = "Outbound export (*.exp)\0*.exp\0\0\0";
  48.     char *my_lpstrTitle = "Outbound export";
  49.     PlatformData *platform;
  50.     int nResult;
  51.     int len = gStuff->filename[0];
  52.     short hlen;
  53.     OPENFILENAME lpofn;
  54.  
  55.     gStuff->filename[len+1] = 0; // make sure null terminated
  56.  
  57.     if (gAliasHandle != NULL)
  58.     {
  59.         /* we've been handed an aliasHandle. Use that instead of querying */
  60.         PIHandle2String(gAliasHandle, my_lpstrInitialDir);
  61.         hlen = my_lpstrInitialDir[0];
  62.  
  63.         if (my_lpstrInitialDir [hlen] == '/' || my_lpstrInitialDir [hlen] == '\\')
  64.         {
  65.             _splitpath(&gStuff->filename[1],
  66.                        NULL, /* drive */
  67.                        NULL, /* dir */
  68.                        gStuff->filename,
  69.                        NULL); /* ext */
  70.             AppendString(my_lpstrInitialDir, gStuff->filename, 0, (short)strlen(gStuff->filename));
  71.             AppendCharToString(my_lpstrInitialDir, '.');
  72.             AppendString(my_lpstrInitialDir, my_lpstrDefExt, 0, (short)strlen(my_lpstrDefExt));
  73.             hlen = my_lpstrInitialDir[0];
  74.         }
  75.         else my_lpstrInitialDir [hlen+1] = 0;
  76.         
  77.         gStuff->filename[ (gStuff->filename[0] = 0)+1 ] = 0;
  78.         AppendString(gStuff->filename, 
  79.                      my_lpstrInitialDir, 1,
  80.                      hlen);
  81.         gStuff->filename[ gStuff->filename[0]+1 ] = 0;
  82.         /* makes string pascal and null terminated */
  83.  
  84.         /* This has to be a Photoshop handle, since it was handed
  85.            to us by the host, so dispose it (it'll be recreated
  86.            later.) */
  87.         PIDisposeHandle((Handle)gAliasHandle);
  88.         gAliasHandle = NULL;
  89.     }
  90.     else
  91.         PICopy (my_lpstrInitialDir, &gStuff->filename[1], (short)gStuff->filename[0]);
  92.     
  93.     _splitpath(&gStuff->filename[1],
  94.                my_lpstrInitialDir, /* drive */
  95.                my_dir, /* dir */
  96.                gStuff->filename,
  97.                my_extension); /* ext */
  98.     strncat (my_lpstrInitialDir, my_dir, 255); 
  99.     
  100.     platform = ((ExportRecordPtr) gStuff)->platformData;
  101.  
  102.     if (!gQueryForParameters)
  103.     {
  104.         strncat(my_lpstrInitialDir, gStuff->filename, 255);
  105.         strncat(my_lpstrInitialDir, ".", 2);
  106.         strncat(my_lpstrInitialDir, my_lpstrDefExt, 5);
  107.         strncpy(gStuff->filename, my_lpstrInitialDir, 255);
  108.         return TRUE;
  109.     }
  110.     else // gQueryForParameters
  111.     {
  112.         /* initialize OPENFILENAME structure */
  113.  
  114.         strncpy(my_filename, gStuff->filename, 255);
  115.         strncat(my_filename, ".", 2);
  116.         strncat(my_filename, my_lpstrDefExt, 5);
  117.  
  118.         lpofn.lStructSize = sizeof(lpofn);
  119.         lpofn.hwndOwner = (HWND)platform->hwnd;
  120.         lpofn.hInstance = hDllInstance;
  121.         lpofn.lpstrFilter = my_lpstrFilter;
  122.         lpofn.lpstrCustomFilter = NULL;
  123.         lpofn.nMaxCustFilter = 0;
  124.         lpofn.nFilterIndex = 0;
  125.         lpofn.lpstrFile = my_lpstrFile;
  126.         lpofn.nMaxFile = 255;
  127.         lpofn.lpstrFileTitle = my_lpstrFileTitle;
  128.         lpofn.nMaxFileTitle = 255;
  129.         lpofn.lpstrInitialDir = my_lpstrInitialDir;
  130.         lpofn.lpstrTitle = my_lpstrTitle;
  131.         lpofn.Flags = OFN_LONGNAMES |
  132.                       OFN_HIDEREADONLY | 
  133.                       OFN_PATHMUSTEXIST |
  134.                       OFN_OVERWRITEPROMPT;
  135.         lpofn.nFileOffset = 0;
  136.         lpofn.nFileExtension = 0;
  137.         lpofn.lpstrDefExt = my_lpstrDefExt;
  138.         lpofn.lCustData = 0;
  139.         lpofn.lpfnHook = NULL;
  140.         lpofn.lpTemplateName = NULL;
  141.         lpofn.lStructSize = sizeof(lpofn);
  142.  
  143.         if (!(nResult = GetSaveFileName(&lpofn)))
  144.             gResult = userCanceledErr;
  145.         else
  146.             gSameNames = (strncmp(my_filename, my_lpstrFileTitle, 255) == noErr);
  147.  
  148.         return (nResult);
  149.     }
  150. }
  151.  
  152.  
  153. /*****************************************************************************/
  154.  
  155. Boolean CreateExportFile (GPtr globals, FileHandle *fRefNum)
  156.     {
  157.     
  158.     OFSTRUCT ostr;
  159.     OSErr  result;
  160.     HFILE file;
  161.     char *filename;
  162.  
  163.     /* Delete the file if it exists. */
  164.  
  165.     *fRefNum = 0;
  166.  
  167.     filename = (char *) gStuff->filename;
  168.     result = OpenFile(filename,&ostr,(WORD)OF_DELETE);
  169.  
  170.     if (result == -1)
  171.         {
  172.         if (ostr.nErrCode != ENOENT)
  173.             {
  174.             gResult = openErr;
  175.             return FALSE;
  176.             }
  177.         }
  178.  
  179.  
  180.     /* Create a new file */
  181.  
  182.     file = OpenFile(filename,&ostr,OF_CREATE|OF_WRITE|OF_SHARE_EXCLUSIVE);
  183.  
  184.     if (file == -1)
  185.         {
  186.         gResult = openErr;
  187.         return FALSE;
  188.         }
  189.  
  190.     else
  191.         {
  192.         *fRefNum = file;
  193.         return TRUE;
  194.         }
  195.  
  196.     }
  197.     
  198.  
  199. /*****************************************************************************/
  200.  
  201. Boolean CloseExportFile (GPtr globals,
  202.                          FileHandle fRefNum)
  203. {
  204.  
  205.     char *filename;
  206.     char my_lpstrInitialDir[256];
  207.     char my_dir[256];
  208.     char *my_extension = ".exp";
  209.     int    len = 0;
  210.  
  211.     OFSTRUCT    ostr;
  212.     
  213.     if (fRefNum != 0)
  214.         _lclose(fRefNum);
  215.  
  216.     /* Delete the file if we had an error. */
  217.  
  218.     if (gResult != noErr)
  219.         {
  220.         filename = (char *) gStuff->filename;
  221.         OpenFile(filename,&ostr,(WORD)OF_DELETE);
  222.         }
  223.         
  224.     /* Mark the file as clean. */
  225.     
  226.     gStuff->dirty = FALSE;
  227.     
  228.     /* Create alias for scripting system */
  229.  
  230.     strncpy(my_lpstrInitialDir, gStuff->filename, 255);
  231.     
  232.     len = strlen(gStuff->filename)+1;
  233.  
  234.     if (gSameNames)
  235.     {
  236.         _splitpath(gStuff->filename,
  237.                    my_lpstrInitialDir, /* drive */
  238.                    my_dir, /* dir */
  239.                    gStuff->filename,
  240.                    my_extension); /* ext */
  241.         strncat(my_lpstrInitialDir, my_dir, 255);
  242.         len = strlen(my_lpstrInitialDir)+1;
  243.     }
  244.         
  245.     my_lpstrInitialDir [len] = 0;
  246.  
  247.     /* If same names, then ignore filename.  Else, leave it in. */
  248.  
  249.     /* uncomment out to make a pascal string
  250.     gStuff->filename[0] = len;
  251.     strncpy(&gStuff->filename[1], my_lpstrInitialDir, len);
  252.     */
  253.     strncpy(gStuff->filename, my_lpstrInitialDir, len); // comment out for pascal
  254.     gStuff->filename[len+1] = 0; // just in case
  255.  
  256.  
  257.     gAliasHandle = PINewHandle(len+1);
  258.     if (gAliasHandle != NULL)
  259.     {
  260.         HLock(gAliasHandle);
  261.         strncpy(*gAliasHandle, gStuff->filename, len+1);
  262.         HUnlock(gAliasHandle);
  263.     }
  264.  
  265.     return TRUE;
  266.     
  267. }
  268.  
  269. /*****************************************************************************/
  270.  
  271. /* File I/O Routines. */
  272.  
  273. OSErr FSWrite(HFILE refNum, long *count, void *buffPtr)
  274. {
  275.     /* Note:  this routine doesn't work for data larger than 64k. */
  276.  
  277.     WORD bytes;
  278.  
  279.     bytes = (WORD)*count;
  280.     if ((*count = _lwrite(refNum,(LPSTR)buffPtr,bytes))==0xffff)
  281.         return writErr;
  282.     else
  283.         return noErr;
  284. }
  285.  
  286. /*****************************************************************************/
  287. /* Dispose alias handle */
  288.  
  289. void DestroyAliasHandle (GPtr globals)
  290. {
  291.     if (gAliasHandle != NULL)
  292.         PIDisposeHandle((Handle)gAliasHandle);
  293.     gAliasHandle = NULL;
  294. }
  295.  
  296. /****************************************************************************/
  297. /* Example for ShowAlert() function which takes a string ID as parameter    */
  298. /* and displays a message box                                               */
  299. /****************************************************************************/
  300.  
  301. short ShowAlert (short stringID)
  302. {
  303.     char szMessage[256];
  304.     char szTitle[128];
  305.  
  306.     LoadString(hDllInstance, stringID, szMessage, sizeof szMessage);
  307.     LoadString(hDllInstance, 2, szTitle, sizeof szTitle);
  308.     return  ( MessageBox(NULL, szMessage, szTitle, MB_OK | MB_ICONHAND) );
  309.  
  310. }
  311.  
  312. /*****************************************************************************/
  313.  
  314. /* Dialog box code to ask for export parameters. */
  315.  
  316. #if defined(__BORLANDC__)
  317. #pragma argsused // lParam in next function not used, so disable warning
  318. #endif
  319.  
  320. BOOL WINAPI ExportProc(HWND hDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)   // Win32 Change
  321. {
  322.      static char *filename;
  323.      int idd;    // Win32 Change
  324.  
  325.     switch  (wMsg)
  326.     {
  327.         case WM_INITDIALOG:
  328.             // Initialize file name with default values
  329.             filename = (char *)lParam;
  330.             StuffText(hDlg, kEditText, filename);
  331.             CenterDialog(hDlg);
  332.  
  333.             return  FALSE;
  334.  
  335.         case WM_COMMAND   :
  336.               idd = COMMANDID (wParam);              // WIN32 Change
  337.             switch  (idd)
  338.             {
  339.                 case ok:
  340.                     FetchText(hDlg, kEditText, filename);
  341.                     /* drop into cancel */
  342.                 case cancel:
  343.                     EndDialog(hDlg, idd);
  344.                     break;
  345.                 default:
  346.                     return FALSE;
  347.             }
  348.             break;
  349.  
  350.         default:
  351.             return  FALSE;
  352.     }
  353.     return  TRUE;
  354. }
  355.  
  356. /*****************************************************************************/
  357.  
  358. /* Initialization and termination code for window's dlls. */
  359.  
  360. // Win32 Change
  361. #ifdef WIN32
  362.  
  363. // Every 32-Bit DLL has an entry point DLLInit
  364.  
  365. BOOL APIENTRY DLLInit(HANDLE hInstance, DWORD fdwReason, LPVOID lpReserved)
  366. {
  367.  
  368.     if (fdwReason == DLL_PROCESS_ATTACH)
  369.         hDllInstance = hInstance;
  370.  
  371.     return TRUE;   // Indicate that the DLL was initialized successfully.
  372. }
  373.  
  374. #else
  375. /* ------------------------------------------------
  376.  *   Code from Borland's window's dll example code.
  377.  * ------------------------------------------------
  378.  */
  379. #if defined(__BORLANDC__)
  380. // Turn off warning: Parameter '' is never used; effects next function only
  381. #pragma argsused
  382. #endif
  383.  
  384. // Every DLL has an entry point LibMain and an exit point WEP.
  385. int FAR PASCAL LibMain( HANDLE hInstance, WORD wDataSegment,
  386.                                    WORD wHeapSize, LPSTR lpszCmdLine )
  387. {
  388.     // Required when using Zortech; causes blink to include startup code
  389.     extern __acrtused_dll;
  390.  
  391.     // The startup code for the DLL initializes the local heap (if there is one)
  392.     // with a call to LocalInit which locks the data segment.
  393.     if ( wHeapSize != 0 )
  394.         UnlockData( 0 );
  395.  
  396.     hDllInstance = hInstance;
  397.     return 1;   // Indicate that the DLL was initialized successfully.
  398. }
  399.  
  400. int FAR PASCAL WEP(int nParam)
  401. {
  402.     switch  (nParam) {
  403.       case  WEP_SYSTEM_EXIT: // System shutdown in progress
  404.       case  WEP_FREE_DLL   : // DLL use count is 0
  405.       default :              // Undefined;  ignore
  406.             return  1;
  407.     }
  408. }
  409. #endif /* WIN32 */